{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Let say\n",
    "\n",
    "Let say, TWTR.csv is my realtime data (follow [realtime-evolution-strategy.ipynb](realtime-evolution-strategy.ipynb)), remember, we trained using `Close`, and `Volume` data.\n",
    "\n",
    "So every request means new daily data.\n",
    "\n",
    "You can improve the code to bind historical data with your own database or any websocket streaming data. Imagination is your limit now."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Adj Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-05-23</td>\n",
       "      <td>32.700001</td>\n",
       "      <td>33.430000</td>\n",
       "      <td>32.599998</td>\n",
       "      <td>33.419998</td>\n",
       "      <td>33.419998</td>\n",
       "      <td>13407500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-05-24</td>\n",
       "      <td>33.439999</td>\n",
       "      <td>33.759998</td>\n",
       "      <td>33.119999</td>\n",
       "      <td>33.520000</td>\n",
       "      <td>33.520000</td>\n",
       "      <td>14491900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-05-25</td>\n",
       "      <td>33.540001</td>\n",
       "      <td>33.990002</td>\n",
       "      <td>33.310001</td>\n",
       "      <td>33.630001</td>\n",
       "      <td>33.630001</td>\n",
       "      <td>10424400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-05-29</td>\n",
       "      <td>33.419998</td>\n",
       "      <td>34.830002</td>\n",
       "      <td>33.349998</td>\n",
       "      <td>34.040001</td>\n",
       "      <td>34.040001</td>\n",
       "      <td>22086700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-05-30</td>\n",
       "      <td>34.200001</td>\n",
       "      <td>34.660000</td>\n",
       "      <td>34.080002</td>\n",
       "      <td>34.360001</td>\n",
       "      <td>34.360001</td>\n",
       "      <td>14588200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Date       Open       High        Low      Close  Adj Close    Volume\n",
       "0  2018-05-23  32.700001  33.430000  32.599998  33.419998  33.419998  13407500\n",
       "1  2018-05-24  33.439999  33.759998  33.119999  33.520000  33.520000  14491900\n",
       "2  2018-05-25  33.540001  33.990002  33.310001  33.630001  33.630001  10424400\n",
       "3  2018-05-29  33.419998  34.830002  33.349998  34.040001  34.040001  22086700\n",
       "4  2018-05-30  34.200001  34.660000  34.080002  34.360001  34.360001  14588200"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('TWTR.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "close = df['Close'].tolist()\n",
    "volume = df['Volume'].tolist()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Check balance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "93.51999599999999"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "requests.get('http://localhost:8005/balance').json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is the initial capital we have for now, you can check [agent.ipynb](https://github.com/huseinzol05/Stock-Prediction-Models/blob/master/realtime-agent/agent.ipynb) how I defined it, or you can overwrite it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Trading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[33.419998, 13407500]'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "data = json.dumps([close[0], volume[0]])\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remember, my last training session was only used `Close` and `Volume`, you need to edit it to accept any kind of parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'action': 'fail',\n",
       " 'balance': 93.51999599999999,\n",
       " 'status': 'data not enough to trade',\n",
       " 'timestamp': '2019-08-31 02:40:10.625022'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "requests.get('http://localhost:8005/trade?data='+data).json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Reason why you got 'data not enough to trade', because, the agent waiting another data to complete the queue, atleast same as `window_size` size.\n",
    "\n",
    "Last time I defined `window_size` is 20, means, it only look back 20 historical data to trade."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Assume now, you have 100 times new datapoints going in, you want to trade these datapoints."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.690977'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.695210'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.699224'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.702625'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.705349'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.708217'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.711154'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.714063'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.716941'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.719288'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.721568'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.723917'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.726292'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.728538'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.730832'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.733636'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.736353'}\n",
      "{'action': 'fail', 'balance': 93.51999599999999, 'status': 'data not enough to trade', 'timestamp': '2019-08-31 02:40:10.738558'}\n",
      "{'action': 'buy', 'balance': 48.56999499999999, 'status': 'buy 1 unit, cost 44.950001', 'timestamp': '2019-08-31 02:40:10.741146'}\n",
      "{'action': 'buy', 'balance': 2.4399939999999916, 'status': 'buy 1 unit, cost 46.130001', 'timestamp': '2019-08-31 02:40:10.743822'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.749816'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.752503'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.757819'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.760163'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.762512'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.764850'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.767202'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.769545'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.771889'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.774284'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.776661'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.779031'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.781386'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.783746'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.786126'}\n",
      "{'action': 'nothing', 'balance': 2.4399939999999916, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.788476'}\n",
      "{'action': 'sell', 'balance': 46.69999199999999, 'gain': -0.6900030000000044, 'investment': -1.5350455720790848, 'status': 'sell 1 unit, price 44.259998', 'timestamp': '2019-08-31 02:40:10.790902'}\n",
      "{'action': 'buy', 'balance': 1.9899929999999912, 'status': 'buy 1 unit, cost 44.709999', 'timestamp': '2019-08-31 02:40:10.793386'}\n",
      "{'action': 'sell', 'balance': 45.329992999999995, 'gain': -2.7900009999999966, 'investment': -6.048126901189525, 'status': 'sell 1 unit, price 43.340000', 'timestamp': '2019-08-31 02:40:10.795829'}\n",
      "{'action': 'sell', 'balance': 88.769992, 'gain': -1.269999999999996, 'investment': -2.8405279096517004, 'status': 'sell 1 unit, price 43.439999', 'timestamp': '2019-08-31 02:40:10.798273'}\n",
      "{'action': 'buy', 'balance': 45.349994, 'status': 'buy 1 unit, cost 43.419998', 'timestamp': '2019-08-31 02:40:10.800601'}\n",
      "{'action': 'nothing', 'balance': 45.349994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.802931'}\n",
      "{'action': 'nothing', 'balance': 45.349994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.805260'}\n",
      "{'action': 'sell', 'balance': 89.569995, 'gain': 0.8000030000000038, 'investment': 1.8424759024632011, 'status': 'sell 1 unit, price 44.220001', 'timestamp': '2019-08-31 02:40:10.807660'}\n",
      "{'action': 'nothing', 'balance': 89.569995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.809990'}\n",
      "{'action': 'nothing', 'balance': 89.569995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.812298'}\n",
      "{'action': 'nothing', 'balance': 89.569995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.814634'}\n",
      "{'action': 'buy', 'balance': 57.699994000000004, 'status': 'buy 1 unit, cost 31.870001', 'timestamp': '2019-08-31 02:40:10.816950'}\n",
      "{'action': 'nothing', 'balance': 57.699994000000004, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.819333'}\n",
      "{'action': 'sell', 'balance': 90.519994, 'gain': 0.9499989999999983, 'investment': 2.980856511425896, 'status': 'sell 1 unit, price 32.820000', 'timestamp': '2019-08-31 02:40:10.821753'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.824076'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.826414'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.828726'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.831048'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.833373'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.835695'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.838029'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.840351'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.842692'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.845018'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.847350'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.849668'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.852011'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.854342'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.856654'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.858977'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.861307'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.863634'}\n",
      "{'action': 'nothing', 'balance': 90.519994, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.865985'}\n",
      "{'action': 'buy', 'balance': 54.879995, 'status': 'buy 1 unit, cost 35.639999', 'timestamp': '2019-08-31 02:40:10.868327'}\n",
      "{'action': 'nothing', 'balance': 54.879995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.870677'}\n",
      "{'action': 'nothing', 'balance': 54.879995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.873026'}\n",
      "{'action': 'nothing', 'balance': 54.879995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.875378'}\n",
      "{'action': 'buy', 'balance': 24.069996000000003, 'status': 'buy 1 unit, cost 30.809999', 'timestamp': '2019-08-31 02:40:10.877719'}\n",
      "{'action': 'buy', 'balance': -6.420003999999995, 'status': 'buy 1 unit, cost 30.490000', 'timestamp': '2019-08-31 02:40:10.880080'}\n",
      "{'action': 'nothing', 'balance': -6.420003999999995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.882496'}\n",
      "{'action': 'nothing', 'balance': -6.420003999999995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.884929'}\n",
      "{'action': 'nothing', 'balance': -6.420003999999995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.887288'}\n",
      "{'action': 'nothing', 'balance': -6.420003999999995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.890380'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'action': 'nothing', 'balance': -6.420003999999995, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.892976'}\n",
      "{'action': 'sell', 'balance': 22.439997000000005, 'gain': -6.7799979999999955, 'investment': -19.023563945666766, 'status': 'sell 1 unit, price 28.860001', 'timestamp': '2019-08-31 02:40:10.896319'}\n",
      "{'action': 'buy', 'balance': -6.7800019999999925, 'status': 'buy 1 unit, cost 29.219999', 'timestamp': '2019-08-31 02:40:10.898795'}\n",
      "{'action': 'sell', 'balance': 22.739998000000007, 'gain': -1.2899989999999946, 'investment': -4.186949178414432, 'status': 'sell 1 unit, price 29.520000', 'timestamp': '2019-08-31 02:40:10.901217'}\n",
      "{'action': 'nothing', 'balance': 22.739998000000007, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.903573'}\n",
      "{'action': 'sell', 'balance': 51.23999800000001, 'gain': -1.990000000000002, 'investment': -6.526730075434576, 'status': 'sell 1 unit, price 28.500000', 'timestamp': '2019-08-31 02:40:10.905993'}\n",
      "{'action': 'sell', 'balance': 79.83999800000001, 'gain': -0.6199989999999964, 'investment': -2.121831010329591, 'status': 'sell 1 unit, price 28.600000', 'timestamp': '2019-08-31 02:40:10.908408'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.910743'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.913068'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.915410'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.917768'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.920095'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.922433'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.924743'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.927077'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.929409'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.931727'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.934065'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.936385'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.938726'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.941060'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.943392'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.945716'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.948038'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.950384'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.952703'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.955028'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.957346'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.959666'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.961985'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.964300'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.966637'}\n",
      "{'action': 'nothing', 'balance': 79.83999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.968961'}\n",
      "{'action': 'buy', 'balance': 45.08999800000001, 'status': 'buy 1 unit, cost 34.750000', 'timestamp': '2019-08-31 02:40:10.971288'}\n",
      "{'action': 'buy', 'balance': 10.469999000000008, 'status': 'buy 1 unit, cost 34.619999', 'timestamp': '2019-08-31 02:40:10.973634'}\n",
      "{'action': 'sell', 'balance': 44.76999800000001, 'gain': -0.4500010000000003, 'investment': -1.294966906474821, 'status': 'sell 1 unit, price 34.299999', 'timestamp': '2019-08-31 02:40:10.976043'}\n",
      "{'action': 'sell', 'balance': 78.78999800000001, 'gain': -0.5999989999999968, 'investment': -1.7330994145898064, 'status': 'sell 1 unit, price 34.020000', 'timestamp': '2019-08-31 02:40:10.978501'}\n",
      "{'action': 'buy', 'balance': 44.37000000000001, 'status': 'buy 1 unit, cost 34.419998', 'timestamp': '2019-08-31 02:40:10.980835'}\n",
      "{'action': 'buy', 'balance': 9.379998000000008, 'status': 'buy 1 unit, cost 34.990002', 'timestamp': '2019-08-31 02:40:10.983215'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.985575'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.987925'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.990264'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.992596'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.994937'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.997459'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:10.999821'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.002180'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.004523'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.006867'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.009205'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.011551'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.013900'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.016258'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.018607'}\n",
      "{'action': 'nothing', 'balance': 9.379998000000008, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.020942'}\n",
      "{'action': 'sell', 'balance': 43.039998000000004, 'gain': -0.7599980000000031, 'investment': -2.2080129115638036, 'status': 'sell 1 unit, price 33.660000', 'timestamp': '2019-08-31 02:40:11.023352'}\n",
      "{'action': 'nothing', 'balance': 43.039998000000004, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.025699'}\n",
      "{'action': 'sell', 'balance': 75.99999700000001, 'gain': -2.0300030000000078, 'investment': -5.8016658587216074, 'status': 'sell 1 unit, price 32.959999', 'timestamp': '2019-08-31 02:40:11.028111'}\n",
      "{'action': 'nothing', 'balance': 75.99999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.030450'}\n",
      "{'action': 'nothing', 'balance': 75.99999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.032773'}\n",
      "{'action': 'nothing', 'balance': 75.99999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.035105'}\n",
      "{'action': 'nothing', 'balance': 75.99999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.037426'}\n",
      "{'action': 'buy', 'balance': 40.10999800000001, 'status': 'buy 1 unit, cost 35.889999', 'timestamp': '2019-08-31 02:40:11.039760'}\n",
      "{'action': 'sell', 'balance': 75.97999700000001, 'gain': -0.01999999999999602, 'investment': -0.05572583047437818, 'status': 'sell 1 unit, price 35.869999', 'timestamp': '2019-08-31 02:40:11.042174'}\n",
      "{'action': 'nothing', 'balance': 75.97999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.044550'}\n",
      "{'action': 'nothing', 'balance': 75.97999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.046900'}\n",
      "{'action': 'buy', 'balance': 43.04999700000001, 'status': 'buy 1 unit, cost 32.930000', 'timestamp': '2019-08-31 02:40:11.049252'}\n",
      "{'action': 'nothing', 'balance': 43.04999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.051631'}\n",
      "{'action': 'buy', 'balance': 15.739998000000014, 'status': 'buy 1 unit, cost 27.309999', 'timestamp': '2019-08-31 02:40:11.053986'}\n",
      "{'action': 'buy', 'balance': -10.710002999999986, 'status': 'buy 1 unit, cost 26.450001', 'timestamp': '2019-08-31 02:40:11.056333'}\n",
      "{'action': 'nothing', 'balance': -10.710002999999986, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.058675'}\n",
      "{'action': 'sell', 'balance': 17.969997000000014, 'gain': -4.25, 'investment': -12.90616459155785, 'status': 'sell 1 unit, price 28.680000', 'timestamp': '2019-08-31 02:40:11.061072'}\n",
      "{'action': 'buy', 'balance': -10.460002999999986, 'status': 'buy 1 unit, cost 28.430000', 'timestamp': '2019-08-31 02:40:11.063421'}\n",
      "{'action': 'nothing', 'balance': -10.460002999999986, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.065750'}\n",
      "{'action': 'sell', 'balance': 18.34999600000001, 'gain': 1.5, 'investment': 5.49249379320739, 'status': 'sell 1 unit, price 28.809999', 'timestamp': '2019-08-31 02:40:11.068168'}\n",
      "{'action': 'sell', 'balance': 46.33999600000001, 'gain': 1.5399989999999981, 'investment': 5.822302237342063, 'status': 'sell 1 unit, price 27.990000', 'timestamp': '2019-08-31 02:40:11.070601'}\n",
      "{'action': 'sell', 'balance': 76.28999700000001, 'gain': 1.520000999999997, 'investment': 5.34646851916988, 'status': 'sell 1 unit, price 29.950001', 'timestamp': '2019-08-31 02:40:11.073017'}\n",
      "{'action': 'nothing', 'balance': 76.28999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.075364'}\n",
      "{'action': 'nothing', 'balance': 76.28999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.077687'}\n",
      "{'action': 'nothing', 'balance': 76.28999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.080016'}\n",
      "{'action': 'nothing', 'balance': 76.28999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.082384'}\n",
      "{'action': 'nothing', 'balance': 76.28999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.084695'}\n",
      "{'action': 'nothing', 'balance': 76.28999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.087023'}\n",
      "{'action': 'nothing', 'balance': 76.28999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.089361'}\n",
      "{'action': 'nothing', 'balance': 76.28999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.091681'}\n",
      "{'action': 'nothing', 'balance': 76.28999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.094002'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'action': 'nothing', 'balance': 76.28999700000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.097499'}\n",
      "{'action': 'buy', 'balance': 44.039997000000014, 'status': 'buy 1 unit, cost 32.250000', 'timestamp': '2019-08-31 02:40:11.100132'}\n",
      "{'action': 'buy', 'balance': 13.069998000000016, 'status': 'buy 1 unit, cost 30.969999', 'timestamp': '2019-08-31 02:40:11.102533'}\n",
      "{'action': 'nothing', 'balance': 13.069998000000016, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.105086'}\n",
      "{'action': 'nothing', 'balance': 13.069998000000016, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.107469'}\n",
      "{'action': 'nothing', 'balance': 13.069998000000016, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.109828'}\n",
      "{'action': 'nothing', 'balance': 13.069998000000016, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.112169'}\n",
      "{'action': 'nothing', 'balance': 13.069998000000016, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.114510'}\n",
      "{'action': 'nothing', 'balance': 13.069998000000016, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.116866'}\n",
      "{'action': 'nothing', 'balance': 13.069998000000016, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.119204'}\n",
      "{'action': 'nothing', 'balance': 13.069998000000016, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.121547'}\n",
      "{'action': 'nothing', 'balance': 13.069998000000016, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.123882'}\n",
      "{'action': 'sell', 'balance': 47.22999800000001, 'gain': 1.9099999999999966, 'investment': 5.922480620155028, 'status': 'sell 1 unit, price 34.160000', 'timestamp': '2019-08-31 02:40:11.126297'}\n",
      "{'action': 'nothing', 'balance': 47.22999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.128652'}\n",
      "{'action': 'nothing', 'balance': 47.22999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.131011'}\n",
      "{'action': 'buy', 'balance': 16.99999800000001, 'status': 'buy 1 unit, cost 30.230000', 'timestamp': '2019-08-31 02:40:11.133356'}\n",
      "{'action': 'nothing', 'balance': 16.99999800000001, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.135699'}\n",
      "{'action': 'sell', 'balance': 48.11999900000001, 'gain': 0.1500020000000042, 'investment': 0.48434615706640544, 'status': 'sell 1 unit, price 31.120001', 'timestamp': '2019-08-31 02:40:11.138112'}\n",
      "{'action': 'sell', 'balance': 79.079998, 'gain': 0.7299989999999994, 'investment': 2.4148164075421743, 'status': 'sell 1 unit, price 30.959999', 'timestamp': '2019-08-31 02:40:11.140527'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.142866'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.145191'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.147519'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.149865'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.152182'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.154505'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.156818'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.159145'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.161472'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.163799'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.166122'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.168434'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.170763'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.173087'}\n",
      "{'action': 'nothing', 'balance': 79.079998, 'status': 'do nothing', 'timestamp': '2019-08-31 02:40:11.175434'}\n",
      "{'action': 'buy', 'balance': 48.209997, 'status': 'buy 1 unit, cost 30.870001', 'timestamp': '2019-08-31 02:40:11.177793'}\n"
     ]
    }
   ],
   "source": [
    "for i in range(200):\n",
    "    data = json.dumps([close[i], volume[i]])\n",
    "    requested = requests.get('http://localhost:8005/trade?data=' + data).json()\n",
    "    print(requested)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
